package com.party.core.service.order.impl;

import com.google.common.base.Strings;
import com.party.common.paging.Page;
import com.party.core.dao.read.order.OrderFormHistoryReadDao;
import com.party.core.dao.write.order.OrderFormHistoryWriteDao;
import com.party.core.model.BaseModel;
import com.party.core.model.order.OrderFormHistory;
import com.party.core.service.order.IOrderFormHistoryService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author yifeng
 * @createTime 2018/1/25 0025 上午 11:57
 * @description 历史订单服务接口实现
 */
@Service
public class OrderFormHistoryService implements IOrderFormHistoryService {
    @Autowired
    private OrderFormHistoryReadDao orderFormHistoryReadDao;

    @Autowired
    private OrderFormHistoryWriteDao orderFormHistoryWriteDao;

    @Override
    public String insert(OrderFormHistory orderFormHistory) {
        BaseModel.preInsert(orderFormHistory);
        boolean result = orderFormHistoryWriteDao.insert(orderFormHistory);
        if (result) {
            return orderFormHistory.getId();
        }
        return null;
    }

    @Override
    public boolean update(OrderFormHistory orderFormHistoryHistory) {
        return false;
    }

    @Override
    public boolean deleteLogic(String id) {
        if (Strings.isNullOrEmpty(id)) {
            return false;
        }
        return orderFormHistoryWriteDao.deleteLogic(id);
    }

    @Override
    public boolean delete(String id) {
        if (Strings.isNullOrEmpty(id)) {
            return false;
        }
        return orderFormHistoryWriteDao.delete(id);
    }

    @Override
    public boolean batchDeleteLogic(Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        return orderFormHistoryWriteDao.batchDeleteLogic(ids);
    }

    @Override
    public boolean batchDelete(Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        return orderFormHistoryWriteDao.batchDelete(ids);
    }

    @Override
    public OrderFormHistory get(String id) {
        return orderFormHistoryReadDao.get(id);
    }

    @Override
    public List<OrderFormHistory> listPage(OrderFormHistory orderFormHistoryHistory, Page page) {
        return orderFormHistoryReadDao.listPage(orderFormHistoryHistory, page);
    }

    @Override
    public List<OrderFormHistory> list(OrderFormHistory orderFormHistoryHistory) {
        return orderFormHistoryReadDao.listPage(orderFormHistoryHistory, null);
    }

    @Override
    public List<OrderFormHistory> batchList(Set<String> ids, OrderFormHistory orderFormHistoryHistory, Page page) {
        return null;
    }

    @Override
    public List<OrderFormHistory> newWebListPage(OrderFormHistory orderFormHistory, Map<String, Object> params) {
        List<OrderFormHistory> orderForms = orderFormHistoryReadDao.newWebListPage(orderFormHistory, params);
        Collections.sort(orderForms, new Comparator<OrderFormHistory>() {
            @Override
            public int compare(OrderFormHistory o1, OrderFormHistory o2) {
                if (o1.getUpdateDate().getTime() > o2.getUpdateDate().getTime()) {
                    return -1;
                } else {
                    return 1;
                }
            }
        });
        return orderForms;
    }

    @Override
    public Integer newWebListCount(OrderFormHistory orderFormHistory, Map<String, Object> params) {
        return orderFormHistoryReadDao.newWebListCount(orderFormHistory, params);
    }

    /**
     * 复制已取消的订单到订单历史表
     *
     * @param orderId 订单id
     */
    @Override
    public boolean copyCanceledOrderForm(String orderId) {
        boolean result = orderFormHistoryWriteDao.copyCanceledOrderForm(orderId);
        return result;
    }
}
